#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@author:atuo.cn
'''

from tkinter import *
from tkinter.ttk import *

from dialogs.dialoghelper import DialogHelper
from dialogs.statusbar import StatusBar
from dialogs.settings import Settings

class ApplicationLayout(Frame, DialogHelper):
    def __init__(self, master, wmtitle):
        Frame.__init__(self, master)
        self.pack(fill=BOTH, expand=1)
        self.wmtitle = wmtitle
        self.winfo_toplevel().title(self.wmtitle)

        self.createWidgets()

    def createWidgets(self):
        self.settings=Settings()
        #menu bar
        self.menubar = Menu(self)
        self.winfo_toplevel().configure(menu=self.menubar)
        filemenu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="File", menu=filemenu)
        settingmenu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Setting", menu=settingmenu)
        convertmenu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label="Convert", menu=convertmenu)
        #menu command
        filemenu.add_command(label="Open", command=self._cmdhandler("open_file"))
        filemenu.add_command(label="Exit", command=self.quit)
        convertmenu.add_command(label="To ePub", command=self._cmdhandler("to_epub"))
        convertmenu.add_command(label="To mobi", command=self._cmdhandler("to_mobi"))
        #settings menu
        settingmenu.add_checkbutton(label="Include Introduction",
                                    variable=self.settings.vars["IncludeIntro"])
        encodingmenu= Menu(settingmenu, tearoff=0)
        settingmenu.add_cascade(label="Source Text Encoding", menu=encodingmenu)
        encodingmenu.add_radiobutton(label="简体中文 gb18030",
                                     value="gb18030",
                                     variable=self.settings.vars["encoding"])
        encodingmenu.add_radiobutton(label="简体中文 GBK",
                                     value="gbk",
                                     variable=self.settings.vars["encoding"])
        encodingmenu.add_separator()                             
        encodingmenu.add_radiobutton(label="繁体中文 BIG5",
                                     value="big5",
                                     variable=self.settings.vars["encoding"])
        encodingmenu.add_separator()
        encodingmenu.add_radiobutton(label="UTF-8",
                                     value="utf-8",
                                     variable=self.settings.vars["encoding"])
        langmenu= Menu(settingmenu, tearoff=0)
        settingmenu.add_cascade(label="Book Language", menu=langmenu)
        langmenu.add_radiobutton(label="Chinese",
                                     value="zh-CN",
                                     variable=self.settings.vars["lang"])        
        langmenu.add_radiobutton(label="English",
                                     value="en-US",
                                     variable=self.settings.vars["lang"])        
        settingmenu.add_separator()
        settingmenu.add_command(label="Performance...", command=self._cmdhandler("edit_settings"))
        #popup menu
        self.popupmenu=Menu(self, tearoff=0)
        self.popupmenu.add_command(label="Edit", command=self._cmdhandler("edit_section_cmd"))
        self.popupmenu.add_command(label="Delete", command=self._cmdhandler("delete_section"))
        self.popupmenu.add_command(label="Insert", command=self._cmdhandler("insert_section"))
        #frame1
        self.frame1 = LabelFrame(self, text="第一步，正则表达式定位标题章节")
        self.frame1.grid(sticky=W+E)
        self.label1 = Label(self.frame1, text="标题章节定位")
        self.sectionRe = StringVar()
        self.entrySectionRe = Entry(self.frame1, textvariable=self.sectionRe, width=100)
        self.buttonSelectRe = Button(self.frame1, text="...")
        self.label1.grid(row=0, column=0)
        self.entrySectionRe.grid(row=0, column=1, sticky=W+E)
        self.buttonSelectRe.grid(row=0,column=2)
        self.label2 = Label(self.frame1, text="标题长度")
        self.label2.grid(row=1, column=0)
        self.titleLen = StringVar()
        frame=Frame(self.frame1)
        frame.grid(row=1,column=1,sticky=W+E)
        self.spinTitleLen = Spinbox(frame, textvariable=self.titleLen,width=2)
        self.spinTitleLen.grid(row=0,column=0, sticky=W)
        self.buttonAdvance=Button(frame, text="高级...")
        self.buttonReopen=Button(frame, text="重新解析")
        self.buttonAdvance.grid(row=0,column=1,padx=10,sticky=W)
        self.buttonReopen.grid(row=0, column=2,padx=10, sticky=W)
        #let column 1 be expandable.
        self.frame1.grid_columnconfigure(1, weight=1)        
        #frame2
        self.frame2 = LabelFrame(self, text="第二步，查看并编辑章节定位内容")
        self.frame2.grid(sticky=E+W+N+S)
        frame2_row = self.frame2.grid_info()["row"]
        self._createSectionView(self.frame2)
        #frame3
        self.frame3 = LabelFrame(self, text="第三步，处理过程选项，确认后按顶部菜单生成eBook")
        self.frame3.grid(sticky=W+E)
        self.captureBookTitle = StringVar()
        self.radioCaptureTitles = [
            Radiobutton(self.frame3, text="从文件名取书名", variable=self.captureBookTitle,value="FILENAME"),
            Radiobutton(self.frame3, text="从内容提取书名", variable=self.captureBookTitle,value="CONTENT"),
        ]
        for i, radio in enumerate(self.radioCaptureTitles): radio.grid(row=0, column=i)
        self.bookTitleRe=StringVar()
        self.entryBookTitileRe = Entry(self.frame3, textvariable=self.bookTitleRe)
        self.entryBookTitileRe.grid(row=0,column=i+1,sticky=W+E)
        self.label3=Label(self.frame3, text="书名")
        self.label3.grid(row=0, column=i+2,sticky=E)
        self.bookTitle=StringVar()
        self.entryBookTitle = Entry(self.frame3, textvariable=self.bookTitle)
        self.entryBookTitle.grid(row=0,column=i+3,sticky=W+E)
        self.captureAuthors = IntVar(value=1)
        self.checkCaptureAuthors = Checkbutton(self.frame3, text="提取作者", variable=self.captureAuthors)
        self.checkCaptureAuthors.grid(row=1,column=i,sticky=W)
        self.authorRe=StringVar()
        self.entryAuthorRe = Entry(self.frame3, textvariable=self.authorRe)
        self.entryAuthorRe.grid(row=1,column=i+1,sticky=W+E)
        self.label4=Label(self.frame3, text="作者")
        self.label4.grid(row=1, column=i+2, sticky=E)
        self.bookAuthors=StringVar()
        self.entryBookAuthors = Entry(self.frame3, textvariable=self.bookAuthors)
        self.entryBookAuthors.grid(row=1,column=i+3,sticky=W+E)
        self.dropHtml=IntVar(value=1)
        self.checkDropHtml=Checkbutton(self.frame3, text="删除垃圾HTML",variable=self.dropHtml)
        self.checkDropHtml.grid(row=2,column=i,sticky=W)
        self.dropHtmlRe=StringVar()
        self.entryDropHtmlRe=Entry(self.frame3, textvariable=self.dropHtmlRe)
        self.entryDropHtmlRe.grid(row=2,column=i+1,sticky=W+E)
        #let column be expandable.
        self.frame3.grid_columnconfigure(i+1, weight=1)
        self.frame3.grid_columnconfigure(i+3, weight=1)        
        #status bar
        self.statusBar = StatusBar(self,[1,3,1,1])
        self.statusBar.grid(sticky=E+W)
        self.statusMsg=StringVar()

        #configure grid size. let row of frame2 be expandable.
        self.rowconfigure(frame2_row, weight=1)
        self.columnconfigure(0, weight=1)

        self._bindEvents()
        self.onLoad()
        self.centerWindow(self.winfo_toplevel())
        
    def _createSectionView(self, master):
        self.label5=Label(master, text="提示：原始文本行与标题相同，则不显示")
        self.label5.grid(row=0,column=0,columnspan=2)
        self.treeviewSections=Treeview(master,
                                       columns=("Title", "Line", "RowText"),
                                       selectmode="extended",
                                       show="headings",
                                       height=20
                                       )
        self.treeviewSections.column("Line", width="20")
        self.treeviewSections.heading("Title", anchor=W, text="标题")
        self.treeviewSections.heading("Line", anchor=W, text="行号")
        self.treeviewSections.heading("RowText", anchor=W, text="原始文本行")
        self.treeviewSections.grid(row=1,column=0,sticky=W+E+N+S)
        self.scrollbarX=Scrollbar(master, orient='horizontal', command=self.treeviewSections.xview)
        self.scrollbarY=Scrollbar(master, orient='vertical', command=self.treeviewSections.yview)
        self.treeviewSections.configure(xscrollcommand=self.scrollbarX.set, yscrollcommand=self.scrollbarY.set)
        self.scrollbarY.grid(row=1,column=1,sticky=N+S)
        self.scrollbarX.grid(row=2,column=0,sticky=W+E)
        master.columnconfigure(0, weight=1)
        master.rowconfigure(1, weight=1)
        
    def _bindEvents(self):
        self.treeviewSections.bind("<Button-3>", self._eventhandler("do_popup"))
        self.treeviewSections.bind("<<TreeviewSelect>>", self._eventhandler("section_selected"))
        self.treeviewSections.bind("<Double-Button-1>", self._eventhandler("edit_section"))
        self.buttonSelectRe.configure(command=self._cmdhandler("select_pattern"))
        self.buttonReopen.configure(command=self._cmdhandler("reopen"))
        self.buttonAdvance.configure(command=self._cmdhandler("advance_hints"))
        
    def onLoad(self):
        pass

